<script>
import request from './api/request.js';

export default {
    async onLaunch() {
        // #ifdef APP-PLUS
        plus.runtime.getProperty(plus.runtime.appid, function (widgetInfo) {
            uni.request({
                url: 'https://cc.wangrunlin.com/update.json',
                data: {
                    version: widgetInfo.version,
                    name: widgetInfo.name
                },
                success: (result) => {
                    var data = result.data;
                    if (data.update && widgetInfo.version !== data.version && data.url) {
                        uni.showModal({
                            title: '更新',
                            content: data.content,
                            confirmText: '更新',
                            success: function (res) {
                                if (res.confirm) {
                                    uni.downloadFile({
                                        url: data.url,
                                        success: (downloadResult) => {
                                            if (downloadResult.statusCode === 200) {
                                                plus.runtime.install(downloadResult.tempFilePath, {
                                                    force: false
                                                }, function () {
                                                    console.log('install success...');
                                                    plus.runtime.restart();
                                                }, function (e) {
                                                    // 这里的错误很重要，最好能记录的服务器日志中，方便调试或以后维护了解更新错误情况，及时解决
                                                    // 如何更新到服务器？
                                                    // 调用一个接口，将e返回咯
                                                    console.error('install fail...');
                                                });
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            });
        });
        // #endif

        let token = uni.getStorageSync('token');
        if (token) {
            let tokenTime = uni.getStorageSync('tokenTime');
            let time_difference = (new Date().getTime() - new Date(tokenTime).getTime()) / 1000 / 60 / 60 / 24;
            if (time_difference > 6 && time_difference < 8) {
                //在这个时间段内重新获取token,重新存储
                let res = await request({
                    url: '/user/token',
                    data: {
                        token: uni.getStorageSync('token')
                    },
                    method: 'POST'
                });
                uni.setStorageSync('token', res[1].data.token);
                uni.setStorageSync('tokenTime', new Date());
            } else if (time_difference > 7) {
                //token存储的时间大于7天，需要重新登录
                uni.removeStorageSync('token');
                uni.removeStorageSync('tokenTime');
                uni.navigateTo({
                    url: 'pages/signin/signin'
                });
            } else {
                this.$store.dispatch('getNotice');
                this.$store.dispatch('getFriends');
                this.$store.dispatch('getAcquire');
                this.$store.dispatch('getUserInfo');
                this.$store.dispatch('getDyNotify');
                this.$store.dispatch('getComNotify');
                this.$store.dispatch('acquireVisitors');
                this.$store.dispatch('getDialogue');
                uni.switchTab({
                    url: './pages/index/index'
                });
            }
        } else {
            uni.navigateTo({
                url: './pages/signin/signin'
            });
        }
    },
    onShow: function () {
        // console.log('App Show')
    },
    onHide: function () {
        // console.log('App Hide')
    }
};
</script>
<style>
/*每个页面公共css */
@import 'https://at.alicdn.com/t/font_1872738_qonhf418ypb.css';
</style>
